package launch;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

import pgui.PVisual;
import rpg.action.Skill;
import rpg.data.CreatureXML;
import rpg.data.EffectXML;
import rpg.data.ItemXML;
import rpg.data.MapXML;
import rpg.data.ScriptXML;
import rpg.data.SkillXML;
import rpg.data.SpriteXML;
import rpg.data.XML;
import rpg.map.Creature;
import rpg.player.Player;
import rpg.visual.VisualMain;
import rpg.visual.VisualMap;
import square.data.CreatureData;
import square.data.DataGroup;
import square.data.SkillData;

/**
 * 
 * @author Pedro Silva Moreira
 */
public class Launch extends JFrame {
	private static final long serialVersionUID = 1L;
	private static Launch launcher;

	// Local Variables
	private Player player;
	private PVisual visual;
	private VisualMain mainComponent;

	// Creation
	public Launch() {
		super();
		
		this.setTitle(LaunchConfig.getName() + " - "
				+ LaunchConfig.getVersion() + " | Created By "
				+ LaunchConfig.getCreatorsCompact());
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		// Global Panel
		JPanel globalPanel = new JPanel();
		this.add(globalPanel);

		globalPanel.setLayout(new BoxLayout(globalPanel, BoxLayout.X_AXIS));

		globalPanel.setSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
		globalPanel.setPreferredSize(new Dimension(Short.MAX_VALUE,
				Short.MAX_VALUE));

		// Visual
		visual = new PVisual(60);
		mainComponent = new VisualMain(visual);
		visual.setMainComponent(mainComponent);

		globalPanel.add(visual);

		visual.maxSize();
		visual.setClip(true);

		visual.setBackground(Color.LIGHT_GRAY);

		this.setMinimumSize(new Dimension(VisualMain.START_WIDTH,
				VisualMain.START_HEIGHT));
		this.setPreferredSize(new Dimension(VisualMain.START_WIDTH,
				VisualMain.START_HEIGHT));
		this.setSize(new Dimension(VisualMain.START_WIDTH,
				VisualMain.START_HEIGHT));
		this.setLocationRelativeTo(null);
	}

	public final void loadData() {
		XML.load(ScriptXML.instance(), "scripts", "script");
		XML.load(SpriteXML.instance(), "sprites", "sprite");
		XML.load(EffectXML.instance(), "effects", "effect");
		XML.load(ItemXML.instance(), "items", "item");
		XML.load(SkillXML.instance(), "skills", "skill");
		XML.load(CreatureXML.instance(), "creatures", "creature");
		XML.load(MapXML.instance(), "maps", "map");

		VisualMap visualMap = ((VisualMain) getVisual().getMainComponent())
				.getMap();
		visualMap.start();

		CreatureData data = new CreatureData();
		data.setName("Pedro");
		data.setDefense(50);
		data.setMaxHealth(5000);
		data.setHealth(5000);
		data.setSprite(DataGroup.findSprite("Wizard"));
		data.setSpeed(100);

		// Player
		Creature playerCreature = new Creature(data);
		playerCreature.addSkill(DataGroup.findSkill("Physical Attack"));
		playerCreature.addSkill(DataGroup.findSkill("Fire Explosion"));

		player = new Player(playerCreature);
		playerCreature.setPlayer(player);
		visualMap.getMap().getLocation(0, 0, 7).setCreature(playerCreature);
		visualMap.setCentralObject(playerCreature);

		// Skills
		int counter = 0;
		for (SkillData it : playerCreature.getData().getSkills()) {
			getMainComponent().getMenu().getSkills()
					.setSkill(counter++, (Skill) it.getOwner());
		}
	}

	// Main
	public static void main(String[] args) {
		launcher = new Launch();
		launcher.setVisible(true);

		launcher.loadData();
	}

	// Getters & Setters
	public static Launch getLauncher() {
		return launcher;
	}

	public static void setLauncher(Launch launcher) {
		Launch.launcher = launcher;
	}

	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}

	public PVisual getVisual() {
		return visual;
	}

	public void setVisual(PVisual visual) {
		this.visual = visual;
	}

	public VisualMain getMainComponent() {
		return mainComponent;
	}

	public void setMainComponent(VisualMain mainComponent) {
		this.mainComponent = mainComponent;
	}
}
